﻿#region Using Directives

    using System;
    using System.Linq;
    using System.Text;

    using Csla;
    using Csla.Data;
    using Csla.Server;
    using Csla.Reflection;

    using MarketPulse.BusinessLogic.Core;
    using MarketPulse.DataAccess;
    using MarketPulse.Reporting.Core;

#endregion

namespace MarketPulse.DataAccess.Factory
{
    /// <summary>
    /// Factory class for producing ReportingPeriodInfoLists
    /// </summary>
    public class ReportingPeriodFactory : ObjectFactory
    {
        #region Member Variables

            /// <summary>
            /// Property that stores the name of the class.
            /// </summary>
            private static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>
            /// Property that stores the log4net logger for this class.
            /// </summary>
            private static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);

        #endregion

        #region Methods

            #region Fetch Methods

                #region ReportingPeriodInfoList

                    /// <summary>
                    /// Method used to fetch a read only list of read only ReportingPeriod objects from the database based on the ClientGUID criteria supplied             
                    /// </summary>
                    /// <param name="clientGuid">The criteria object holding the ClientGUID used to retrieve the list</param>
                    /// <returns>A read only list of read only ReportingPeriod objects retrieved from the database</returns>
                    public ReportingPeriodInfoList FetchInfoList(SingleCriteria<Guid> clientGuid)
                    {
                        try
                        {
                            using (var manager = ContextManager<DataContextObjects.Reporting.MUMReporting_dev>.GetManager(Database.ReportingSystem))
                            {
                                // Create an instance to return
                                var reportingPeriodInfoList = (ReportingPeriodInfoList)MethodCaller.CreateInstance(typeof(ReportingPeriodInfoList));

                                // Fetch the data from the database based on the criteria
                                var selectResult = manager.DataContext.UspReportingPeriodSelectByClientGUID(clientGuid.Value);

                                // ReportingPeriodInfoList is a read-only collection,
                                // so typically, when the data access is implemented in
                                // DataPortal_Fetch within the business object, the
                                // private IsReadOnly property must be set false in order to 
                                // populate the collection.  Here, within a factory 
                                // , it isn't visible so one must resort to calling the 
                                // SetIsReadOnly method provided by our parent, ObjectFactory.
                                SetIsReadOnly(reportingPeriodInfoList, false);
                                foreach (DataContextObjects.Reporting.UspReportingPeriodSelectByClientGUIDResult result in selectResult)
                                {
                                    ReportingPeriodInfo reportingPeriodInfo = (ReportingPeriodInfo)MethodCaller.CreateInstance(typeof(ReportingPeriodInfo));

                                    // Common properties inherited from MarketPulseBase<Branch>
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.GUIDProperty, result.ReportingPeriodGUID);
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.ObsoleteProperty, result.Obsolete.Value);
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.InsertDateTimeProperty, (SmartDate)result.InsertDateTime);
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.InsertUserGUIDProperty, result.InsertUserGUID.Value);
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.UpdateDateTimeProperty, (SmartDate)result.UpdateDateTime);
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.UpdateUserGUIDProperty, result.UpdateUserGUID.Value);
                                    // Other properties
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.ReportingPeriodTypeGUIDProperty, result.ReportingPeriodTypeGUID);
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.ReportingPeriodNameProperty, result.ReportingPeriodName.ToString());
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.EffectiveStartDateTimeProperty, result.EffectiveStartDateTime);
                                    LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.EffectiveEndDateTimeProperty, result.EffectiveEndDateTime);

                                    if (result.CompanyGUID.HasValue)
                                    {
                                        LoadProperty(reportingPeriodInfo, ReportingPeriodInfo.CompanyGUIDProperty, result.CompanyGUID);
                                    }

                                    //CheckRules(reportingPeriodInfo);

                                    // Make sure that CSLA knows this has changed since being instantiated
                                    MarkOld(reportingPeriodInfo);

                                    // Add the item to the list
                                    reportingPeriodInfoList.Add(reportingPeriodInfo);
                                }
                                SetIsReadOnly(reportingPeriodInfoList, true);

                                // Make sure that CSLA knows this has changed since being instantiated
                                MarkOld(reportingPeriodInfoList);

                                return reportingPeriodInfoList;
                            }
                        }
                        catch (InvalidOperationException ioe)
                        {
                            mLog.Error(ioe.ToString());
                            throw ioe;
                        }
                        catch (Exception ex)
                        {
                            mLog.Error(ex.ToString());
                            throw ex;
                        }
                    }

                #endregion

            #endregion

        #endregion

    }
}
